

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Han Qiu">
  <meta name="keywords" content="">
  
    <meta name="description" content="看到vue文档的示例中，使用以下代码创建了2个相同的段落  注：文档中length的值为20，这里方便演示改为了2  1234567render: function (createElement) &amp;#123;     return createElement(&amp;#x27;div&amp;#x27;,         Array.apply(null, &amp;#123; length: 2 &amp;#125;).m">
<meta property="og:type" content="article">
<meta property="og:title" content="Array.apply(null, { length: 2 })是什么意思">
<meta property="og:url" content="http://example.com/2022/01/26/Array-apply-null-length-2-%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/index.html">
<meta property="og:site_name" content="寒秋の博客">
<meta property="og:description" content="看到vue文档的示例中，使用以下代码创建了2个相同的段落  注：文档中length的值为20，这里方便演示改为了2  1234567render: function (createElement) &amp;#123;     return createElement(&amp;#x27;div&amp;#x27;,         Array.apply(null, &amp;#123; length: 2 &amp;#125;).m">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/js/Array.apply(null,%20%7B%20length%202%20%7D)%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84.png">
<meta property="og:image" content="https://gitee.com/huqian025/my-images/raw/master/js/Array.apply(null,%20%7B%20length%202%20%7D)%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/%E8%B0%83%E7%94%A8map.png">
<meta property="article:published_time" content="2022-01-26T03:56:41.000Z">
<meta property="article:modified_time" content="2022-01-26T07:05:02.566Z">
<meta property="article:author" content="Han Qiu">
<meta property="article:tag" content="js">
<meta property="article:tag" content="vue">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://gitee.com/huqian025/my-images/raw/master/js/Array.apply(null,%20%7B%20length%202%20%7D)%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84.png">
  
  
  <title>Array.apply(null, { length: 2 })是什么意思 - 寒秋の博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Array.apply(null, { length: 2 })是什么意思">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-01-26 11:56" pubdate>
        2022年1月26日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.2k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      10 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Array.apply(null, { length: 2 })是什么意思</h1>
            
            <div class="markdown-body">
              <p>看到vue文档的示例中，使用以下代码创建了2个相同的段落</p>
<blockquote>
<p>注：文档中length的值为20，这里方便演示改为了2</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><pre><code class="hljs js">render: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">createElement</span>) </span>&#123; <br>    <span class="hljs-keyword">return</span> createElement(<span class="hljs-string">&#x27;div&#x27;</span>, <br>        <span class="hljs-built_in">Array</span>.apply(<span class="hljs-literal">null</span>, &#123; <span class="hljs-attr">length</span>: <span class="hljs-number">2</span> &#125;).map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123; <br>            <span class="hljs-keyword">return</span> createElement(<span class="hljs-string">&#x27;p&#x27;</span>, <span class="hljs-string">&#x27;hi&#x27;</span>) <br>        &#125;) <br>    ) <br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="1-apply方法中第二个参数-length-2-是什么意思"><a href="#1-apply方法中第二个参数-length-2-是什么意思" class="headerlink" title="1. apply方法中第二个参数{length: 2}是什么意思"></a>1. apply方法中第二个参数{length: 2}是什么意思</h4><p><strong>对象{length: 2}是一个类数组对象，因为没有初始化下标0，1的值，所以获取0，1下标的值得到的都是undefined。</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 类数组对象可以转成真正的数组</span><br><span class="hljs-keyword">const</span> a = <span class="hljs-built_in">Array</span>.prototype.slice.call(&#123;<span class="hljs-attr">length</span>: <span class="hljs-number">2</span>&#125;);<br><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Array</span>.isArray(a)) <span class="hljs-comment">// true</span><br></code></pre></td></tr></table></figure>

<h4 id="2-Array-apply-null-length-2-是什么意思"><a href="#2-Array-apply-null-length-2-是什么意思" class="headerlink" title="2. Array.apply(null, { length: 2 })是什么意思"></a>2. Array.apply(null, { length: 2 })是什么意思</h4><p><strong>ES5开始apply函数的第二个参数除了可以是数组外，还可以是类数组对象（即包含length属性，且length属性值是个数字的对象）</strong></p>
<p>直接调用Array函数和new方式调用是等价的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js">cosnt a = <span class="hljs-built_in">Array</span>(<span class="hljs-number">2</span>) <span class="hljs-comment">// 等价于const a = new Array(2)</span><br></code></pre></td></tr></table></figure>

<p>因此Array.apply(null, { length: 2 })就是创建一个存储两个undefined的数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-built_in">Array</span>.apply(<span class="hljs-literal">null</span>, &#123; <span class="hljs-attr">length</span>: <span class="hljs-number">2</span> &#125;)<br><span class="hljs-comment">// 等价于</span><br><span class="hljs-built_in">Array</span>.apply(<span class="hljs-literal">null</span>, [<span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>])<br><span class="hljs-comment">// 等价于</span><br><span class="hljs-built_in">Array</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>)<br><span class="hljs-comment">// 等价于</span><br><span class="hljs-keyword">new</span> <span class="hljs-built_in">Array</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-literal">undefined</span>)<br></code></pre></td></tr></table></figure>

<h4 id="3-Array-2-与Array-apply-null-length-2-的区别"><a href="#3-Array-2-与Array-apply-null-length-2-的区别" class="headerlink" title="3. Array(2)与Array.apply(null, { length: 2 })的区别"></a>3. Array(2)与Array.apply(null, { length: 2 })的区别</h4><ul>
<li>相同点：都是创建一个长度为2的数组</li>
<li>不同点：<strong>Array(2)创建的数组元素没有被初始化，Array.apply(null, { length: 2 })创建的数组每个元素都被赋值为undefined。</strong></li>
</ul>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/js/Array.apply(null,%20%7B%20length%202%20%7D)%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/%E5%88%9B%E5%BB%BA%E6%95%B0%E7%BB%84.png" srcset="/img/loading.gif" lazyload alt="创建数组"></p>
<h4 id="4-vue文档中为什么用Array-apply-null-length-2"><a href="#4-vue文档中为什么用Array-apply-null-length-2" class="headerlink" title="4. vue文档中为什么用Array.apply(null, { length: 2 })"></a>4. vue文档中为什么用Array.apply(null, { length: 2 })</h4><p><strong>map函数不会遍历数组中没有初始化或者被delete的元素</strong>（有相同限制还有forEach, reduce方法），根据上述“Array(2)与Array.apply(null, { length: 2 })的区别”可以得知，Array(2)创建的数组map的回调函数不会执行，Array.apply(null, { length: 2 })创建的数组map的回调函数才会执行两次</p>
<p><img src="https://gitee.com/huqian025/my-images/raw/master/js/Array.apply(null,%20%7B%20length%202%20%7D)%E6%98%AF%E4%BB%80%E4%B9%88%E6%84%8F%E6%80%9D/%E8%B0%83%E7%94%A8map.png" srcset="/img/loading.gif" lazyload alt="调用map"></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/js/">js</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/js/">js</a>
                    
                      <a class="hover-with-bg" href="/tags/vue/">vue</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/01/26/%E8%AE%B0%E4%B8%80%E6%AC%A1%E4%BD%BF%E7%94%A8Hexo%E6%90%AD%E5%BB%BAblog/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">记一次使用Hexo + GitHub Pages搭建blog</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/01/10/%E6%8C%89%E4%BD%8D%E5%8F%96%E5%8F%8D/">
                        <span class="hidden-mobile">按位取反(~)</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
